# -*- coding: UTF-8 -*-
from api import client
from common.route import *
import os
import configparser
from until.ServiceTime_random import order_time_choose
# from common.read_config import conf

data = get_yaml_filepath(Environ='data.yaml')  # 读取config下的data数据
class Order_action(client.HttpClient):
    orderNo = {}
    orderId = {}
    serviceTime = {}
    timeQuantum = {}

    def __init__(self,token):
        super().__init__()
        self.host = data['admin_host']
        '''初始化获取token'''
        config_path = os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirname(__file__)))
                                      + r'\configs\token.ini')
        conf_token = configparser.ConfigParser()
        conf_token.read(config_path, encoding='utf-8-sig')
        test_token = conf_token.get('token', '{}'.format(token))
        self.token = test_token

    """获取所有产品"""
    def read_product_list(self):

        url = product_list['url']
        url = self.get_full_url(url)
        method = product_list['method']
        return self.send(url, method=method, token=self.token)

    """获取库存"""
    def get_order_time(self,attributes,productId,latitude,longitude):
        """

        :param attributes: 产品属性
        :param productId: 产品id
        :param latitude: 纬度
        :param longitude: 经度
        :return:请求返回值
        """
        url = order_time_list['url']
        url = self.get_full_url(url)
        method = order_time_list['method']
        body = {"attributes":attributes,
                "productId":productId,
                "latitude":latitude,
                "longitude":longitude,
                "length":10}
        result = self.send(url,method=method,body=body,token=self.token)
        # 获取可用的库存时间点
        available_order_time = order_time_choose(result)
        # print('时间结果列表',available_order_time)
        serviceTime = available_order_time['time2']
        timeQuantum = available_order_time['time1']
        # 将可用时间赋值给类属性
        self.serviceTime = serviceTime
        self.timeQuantum = timeQuantum
        return result

    """创建订单"""
    def create_order(self,
                     productId,
                     addressId,
                     # serviceTime,
                     # timeQuantum,
                     attrs_id,
                     attrs_vaule,
                     source,payType):
        """
        :param productId: 产品id
        :param addressId: 地址id
        :param attrs_id: 产品属性id
        :param attrs_vaule: 产品属性value
        :param source:
        :param payType:
        :return:
        """
        url = order_list['url']
        url = self.get_full_url(url)
        method = order_list['method']
        body = {"productId":productId,
                "addressId":addressId,
                "discountId":0,
                "serviceTime":self.serviceTime,
                "timeQuantum":self.timeQuantum,
                "specials":[],
                "attrs":[{"id":attrs_id,"value":attrs_vaule}],
                "enableAssign":False,
                "source":source,
                "payType":payType,
                "remark":""}
        result = self.send(url,method=method,body=body,token=self.token)

        orderNo = result['body']['orderNo']
        orderId = result['body']['orderId']

        # 将订单号及id赋值给类属性
        self.orderNo = orderNo
        self.orderId = orderId
        return result

    """支付订单"""
    def pay_order(self):
        url = order_pay_list['url']
        url = self.get_full_url(url,replace=self.orderNo)
        method = order_pay_list['method']
        return self.send(url=url,method=method,token=self.token)

    """取消订单"""
    def cancel_order(self,remark):
        """

        :param remark: 取消备注
        :return:
        """

        url = order_cancel_list['url']
        url = self.get_full_url(url,replace=self.orderId)
        method = order_cancel_list['method']
        body = {"remark":remark}
        return self.send(url=url,method=method,body=body,token=self.token)